<?
$this->Html->scriptBlock($scriptDate, array('inline' => false));
$this->Html->scriptBlock($scriptDateDefault, array('inline' => false));
?>
<div class="title-body-content"><?php echo $title_for_layout; ?></div>
<?php
$formUrl = array('controller' => 'brokinginquiries', 'action' => 'add');
echo $this->Form->create('Brokinginquiry', array('url' => $formUrl, 'type' => 'file'));
?>
<div class="wrapper-form-broking clearfix">
    <div class="wrapper-broking-inquyri clearfix">
        <div class="wrapper-inquirer clearfix">
            <span class="sp-inquirer"><?php echo __('Risk Reference #'); ?> :</span>
            <?php
            echo $this->Form->input('risk_ref', array(
                'value' => 'RISK-'.strtoupper(uniqid()),
                'class' => 'sel-inquirer',
                //'readonly' => 'readonly',
                'label' => false,
                'div' => false,)
            );
            ?>
            <span id="exsisterror"></span>
        </div>
        <div class="wrapper-inquirer clearfix">
            <span class="sp-inquirer"><?php echo __('Producer'); ?> :</span>
            <?php
            echo $this->Form->input('Inquirer.Inquirer', array(
                'multiple' => false,
                'empty' => false,
                'class' => 'sel-inquirer-select',
                'label' => false,
                'div' => false,)
            );
            ?>
        </div>
        <div class="wrapper-inquirer clearfix">
            <span class="sp-inquirer"><?php echo __('Account Name'); ?> (*):</span>
            <?php
            echo $this->Form->text('account_name', array(
                'class' => 'sel-inquirer',
                'label' => false,
                'div' => false,)
            );
            ?>
        </div>
        <div class="wrapper-insurance-period-page4 clearfix">
            <span class="sp-inquirer"><?php echo __('Period'); ?> (*):</span>
            <?php
            echo $this->Form->text('date_from', array(
                'class' => 'sel-date',
                'readonly' => 'readonly',
                'label' => false,
                'div' => false,)
            );
            ?>
            <span class="sp-to"><?php echo __('To'); ?></span>
            <?php
            echo $this->Form->text('date_to', array(
                'class' => 'sel-date',
                'readonly' => 'readonly',
                'label' => false,
                'div' => false,)
            );
            ?>
        </div>
        <div class="wrapper-file-attach clearfix">
            <span class="sp-file-attach"><?php echo __('File Attached'); ?> (*):</span>
            <?php echo $this->Form->input('file', array('label' => false, 'div' => false, 'type' => 'file',)); ?>
            <span style="font-style: italic;">Allow file format: *.pdf *.doc, *.docx, *.xls, *.xlsx, *.txt.</span>
        </div>
    </div>
</div>
<!-- HTML for buttom -form-->
<div class="wrapper-button-form">
    <div class="button-form">
        <div class="button-aihre button-blue"><a href="#" class="save">SAVE</a></div>
        <div class="button-aihre button-blue"><?php echo $this->Html->link(__('CANCEL'), array('plugin' => null, 'controller' => 'brokinginquiries', 'action' => 'index')); ?></div>
    </div>
</div>
<?php
echo $this->Form->end();
?>
<?php
$validForm = "
    $(function(){
        $('a.save').click(function(){
            $.getJSON('" . Router::url('/admin/brokinginquiries/check_unique/', true) . "'+$('#BrokinginquiryRiskRef').val(), function(data) {
                if(data.success){
                    if(data.isExsist === '1'){
                        $('#exsisterror').html('This Risk Reference has existed.');
                        $('#exsisterror').attr('style','color:red');
                    }else{
                        $('#exsisterror').removeAttr('style');
                        $('#exsisterror').html('');
                        var startDt=$('#BrokinginquiryDateFrom').val();
                        var endDt=$('#BrokinginquiryDateTo').val();
                        if($('#BrokinginquiryRiskRef').val() == ''){
                            alert('Risk Reference must be not empty.');
                        }else if($('#InquirerInquirer').val() == ''){
                            alert('Producer must be not empty.');
                        }else if($('#BrokinginquiryAccountName').val() == ''){
                            alert('Account name must be not empty.');
                        }else if((new Date(startDt).getTime() >= new Date(endDt).getTime())){
                            alert('To date is invalid. Must be greater than date from.');
                        }else{
                            $('#BrokinginquiryAdminAddForm').submit();
                        }
                    }
                } else {
                    // error
                }
            });
        });
    });";
$this->Html->scriptBlock($validForm, array('inline' => false));
?>